package com.smartum.mpr.dao;


import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.smartum.mpr.dto.Filter;
import com.smartum.mpr.exception.DatabaseIntegrityException;
import com.smartum.mpr.exception.NotExistsUserException;
import com.smartum.mpr.model.User;
import com.smartum.mpr.model.UsersFriends;
import com.smartum.mpr.model.UsersFriendsRequest;

@Repository
public class UserSearchDao extends GenericDao<Long, User> {
  Logger logger =Logger.getLogger(UserSearchDao.class);
	@Override
	protected Class<User> getEntityClass() {
		return User.class;
	}
	
	public void saveRequest(UsersFriendsRequest solicitud){
	hibernateTemplate.saveOrUpdate(solicitud);
	}
	
	public List<User> findListUserFriendsRequest(User user) throws DatabaseIntegrityException, NotExistsUserException {
		DetachedCriteria criteria = DetachedCriteria.forClass(UsersFriendsRequest.class);
		criteria.add(Restrictions.eq("usersByUserId", user))
		                                 .add(Restrictions.eq("status", 0));           /**Amigo Activo**/
		List<UsersFriendsRequest> usersRequests = new ArrayList<UsersFriendsRequest>();
		                   usersRequests=hibernateTemplate.findByCriteria(criteria);
		
		List<User> lstRetorno=new ArrayList<User>();
		for (UsersFriendsRequest user2 : usersRequests) {
			lstRetorno.add(user2.getUsersByUserIdFriend());
		}
				   	 
				   
		                   
		if (lstRetorno.size() == 0) {
		    throw new NotExistsUserException();
		}else{
			return lstRetorno;
		}
		
	}
	public List<UsersFriends> findListUserFriendsByUser(User user) throws DatabaseIntegrityException, NotExistsUserException {
		DetachedCriteria criteria = DetachedCriteria.forClass(UsersFriends.class);
		criteria.add(Restrictions.eq("usersByUserId", user))
		                                 .add(Restrictions.eq("status", 0));           /**Amigo Activo**/
		List<UsersFriends> usersfriends = new ArrayList<UsersFriends>();
		                   usersfriends=hibernateTemplate.findByCriteria(criteria);
		
		if (usersfriends.size() == 0) {
		    throw new NotExistsUserException();
		}else{
			return usersfriends;
		}
		
	}
	public List<UsersFriends> findListUserFriendsYouMayknowByUser(List<UsersFriends> myUsersfriends) throws DatabaseIntegrityException, NotExistsUserException {

		List<User> lstMyFriends = new ArrayList<User>();
		
		for (UsersFriends friends : myUsersfriends) {
			   lstMyFriends.add(friends.getUsersByUserFriendId());
		     }     
		   
		System.out.println("lstMyFriends.size : " + lstMyFriends!=null?lstMyFriends.size():lstMyFriends);                   
		               
		DetachedCriteria criteria = DetachedCriteria.forClass(UsersFriends.class);
		criteria.add(Restrictions.in("usersByUserId", lstMyFriends))
			                       .add(Restrictions.eq("status", 0))
			                       .add(Restrictions.not(Restrictions.in("usersByUserFriendId", lstMyFriends)));
		criteria.add(Restrictions.sqlRestriction("1=1 order by rand() LIMIT 25"));
		List<UsersFriends> yoursUsersfriends = new ArrayList<UsersFriends>();
		yoursUsersfriends=hibernateTemplate.findByCriteria(criteria); 
		System.out.println("yoursUsersfriends.size : " + yoursUsersfriends!=null?yoursUsersfriends.size():yoursUsersfriends);    
		
		if (yoursUsersfriends.size() == 0) {
		    throw new NotExistsUserException();
		}else{
			return yoursUsersfriends;
		}
		
	}
	
	public List<User> findListUserFriendsSearch(User user) throws DatabaseIntegrityException, NotExistsUserException {
		logger.debug(" user.getCountry()  " + user.getCountry()!=null?user.getCountry().getId():user.getCountry());
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		criteria.add(Restrictions.not(Restrictions.eq("id", user.getId())));
		criteria.add(Restrictions.not(Restrictions.eq("country.id", user.getCountry().getId())));
		criteria.add(Restrictions.sqlRestriction("1=1 order by rand() LIMIT 25"));
		
		
		List<User> yoursUsersfriends = new ArrayList<User>();
		yoursUsersfriends=hibernateTemplate.findByCriteria(criteria); 
		System.out.println("yoursUsersfriends.size : " + yoursUsersfriends!=null?yoursUsersfriends.size():yoursUsersfriends);    
		
		if (yoursUsersfriends.size() == 0) {
		    throw new NotExistsUserException();
		}else if(yoursUsersfriends.size() <25 ){
			List<Long> yoursUsersfriendsOtherCountry=new  ArrayList<Long>();
			for (User userNoCountry : yoursUsersfriends) {
				yoursUsersfriendsOtherCountry.add(userNoCountry.getId());
			}
			DetachedCriteria criteria2 = DetachedCriteria.forClass(User.class);
			criteria2.add(Restrictions.not(Restrictions.eq("id", user.getId())));
			criteria2.add(Restrictions.not(Restrictions.in("id",yoursUsersfriendsOtherCountry)));
			criteria2.add(Restrictions.sqlRestriction("1=1 order by rand() LIMIT 25"));
			
			List<User> yoursUsersfriendsNoCountry = new ArrayList<User>();
			yoursUsersfriendsNoCountry=hibernateTemplate.findByCriteria(criteria2);
			
			if(yoursUsersfriendsNoCountry.size()>0 ){
				yoursUsersfriends.addAll(yoursUsersfriendsNoCountry);
			 }
		}
		return yoursUsersfriends;
		
	}
	public List<User> findListUserFriendsFilterByUser(User user, Filter filter) throws DatabaseIntegrityException, NotExistsUserException {

		List<User> lstMyFriends = new ArrayList<User>();
		
		
		System.out.println("lstMyFriends.size : " + lstMyFriends!=null?lstMyFriends.size():lstMyFriends);                   
		               
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		criteria.add(Restrictions.not(Restrictions.eq("id", user.getId())));
		criteria.add(Restrictions.between("age", filter.getAgeMin(),filter.getAgeMax()));
		criteria.add(Restrictions.eq("gender", filter.getGender()));
		criteria.add(Restrictions.eq("country", filter.getCountry()));		
		criteria.add(Restrictions.sqlRestriction("1=1 order by rand() LIMIT 25"));
		
		List<User> yoursUsersfriends = new ArrayList<User>();
		yoursUsersfriends=hibernateTemplate.findByCriteria(criteria); 
		System.out.println("yoursUsersfriends.size : " + yoursUsersfriends!=null?yoursUsersfriends.size():yoursUsersfriends);    
		
		if (yoursUsersfriends.size() == 0) {
		    throw new NotExistsUserException();
		}else{
			return yoursUsersfriends;
		}
		
	}
	

}
